package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.pojo.*;
import com.tbit.uqbike.object.pojo.dto.ActivityDTO;
import com.tbit.uqbike.object.pojo.vo.ActivityVO;
import com.tbit.uqbike.object.pojo.vo.AreaVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.ActivityService;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * 活动管理
 *
 * @Author:BUGTian
 * @DATE: 2022/5/17
 */
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private AccountService accountService;

    /**
     * 添加邀请新用户活动
     *
     * @param activityDTO
     * @return
     */
    @PostMapping("/add")
    @OperateLog(module = ModuleEnum.ACTIVITYCONFIG, methods = MethodEnum.ACTIVITYCONFIG_ADD)
    public Result addActivity(@RequestBody ActivityDTO activityDTO) {
        return activityService.addActivity(activityDTO);
    }

    /**
     * 修改邀请新用户活动
     *
     * @param activityDTO
     * @return
     */
    @PutMapping("/update")
    @OperateLog(module = ModuleEnum.ACTIVITYCONFIG, methods = MethodEnum.ACTIVITYCONFIG_UPDATE)
    public Result updateActivity(@RequestBody ActivityDTO activityDTO) {
        return activityService.update(activityDTO);
    }


    /**
     * 删除活动
     *
     * @param id 活动主键id
     * @return
     */
    @DeleteMapping("/delete")
    @OperateLog(module = ModuleEnum.ACTIVITYCONFIG, methods = MethodEnum.ACTIVITYCONFIG_DEL)
    public Result deleteActivity(Integer id) {
        return activityService.deleteActivity(id);
    }

    /**
     * 根据主键查询活动详情
     *
     * @param id 活动主键
     * @return
     */
    @GetMapping("/queryById")
    public Result<ActivityVO> queryById(Integer id) {
        return activityService.queryById(id);
    }

    /**
     * 分页查询活动列表
     *
     * @param page      当前页码
     * @param size      每页显示的行
     * @param accountId 区域id
     * @param isvalid   是否有效 (0:无效，1:生效，2:历史生效)
     * @return
     */
    @GetMapping("/queryPage")
    public Result<PageResult<Activity>> queryPage(Integer page, Integer size, Integer accountId, Integer isvalid) {
        PageResult<Activity> pageResult = activityService.queryPage(page, size, accountId, isvalid);
        return Result.success(pageResult);
    }

    /**
     * 活动开关
     *
     * @param id 活动主键id
     * @return
     */
    @PutMapping("/activitySwitch")
    @OperateLog(module = ModuleEnum.ACTIVITYCONFIG, methods = MethodEnum.ACTIVITYCONFIG_SWITCH)
    public Result activitySwitch(Integer id) {
        return activityService.activitySwitch(id);
    }

    /**
     * 活动统计次数图表
     *
     * @param accountId   品牌id
     * @param adAccountId 区域id
     * @param startTime   开始时间
     * @param endTime     结束时间
     * @param eventType   活动类型
     * @return
     */
    @GetMapping("/getActiveStatCount")
    public Result<List<ActiveStat>> activeStatCount(Integer accountId, Integer adAccountId, @NotNull String startTime, @NotNull String endTime, Integer eventType) {
        return activityService.activeStatCount(accountId, adAccountId, startTime, endTime, eventType);
    }

    /**
     * 活动统计次数分页
     *
     * @param accountId   品牌id
     * @param adAccountId 区域id
     * @param startTime   开始时间
     * @param endTime     结束时间
     * @param eventType   活动类型
     * @param page        当前页码
     * @param size        每页显示的行
     * @return
     */
    @GetMapping("/getActiveStatCountPage")
    public Result<PageResult<ActiveStat>> activeStatCountPage(Integer accountId, Integer adAccountId, @NotNull String startTime, @NotNull String endTime, Integer eventType, @NotNull Integer page, @NotNull Integer size) {
        return activityService.activeStatCountPage(accountId, adAccountId, startTime, endTime, eventType, page, size);
    }

    /**
     * 活动统计次数导出
     *
     * @param accountId   品牌id
     * @param adAccountId 区域id
     * @param startTime   开始时间
     * @param endTime     结束时间
     * @param eventType   活动类型
     * @return
     */
    @GetMapping("/exportActiveStatCount")
    public Result exportActiveStatCount(Integer accountId, Integer adAccountId, @NotNull String startTime, @NotNull String endTime, Integer eventType) {
        Result<PageResult<ActiveStat>> result = activityService.activeStatCountPage(accountId, adAccountId, startTime, endTime, eventType, 1, 65535);
        List<ActiveStat> list = result.getData().getList();
        ExcelUtil<ActiveStat> excel = new ExcelUtil<>(ActiveStat.class);
        JsonResult activeStat = excel.exportExcel(list, "活动统计次数");
        return Result.success(activeStat.getData());
    }

    /**
     * 活动统计人数
     *
     * @param accountId   品牌id
     * @param adAccountId 区域id
     * @param eventType   活动类型
     * @return
     */
    @GetMapping("/activeCountNumber")
    public Result<List<EvenJoinDetail>> activeCountNumber(Integer accountId, Integer adAccountId, Integer eventType) {
        List<EvenJoinDetail> evenJoinDetails = activityService.activeCountNumber(accountId, adAccountId, eventType);
        return Result.success(evenJoinDetails);
    }

    /**
     * 奖励统计明细
     *
     * @param page        当前页码
     * @param size        每页显示的行
     * @param adAccountId 区域id
     * @param accountId   品牌id
     * @param startTime   开始时间
     * @param endTime     结束时间
     * @param invitePhone 用户手机号
     * @param eventId     活动id
     * @param eventType   活动类型
     * @param rewardName  优惠券名称
     * @return
     */
    @GetMapping("/queryUserRewardLog")
    public Result<PageResult<UserRewardLog>> queryUserRewardLog(@NotNull Integer page, @NotNull Integer size, Integer adAccountId, Integer accountId, Date startTime, Date endTime, String invitePhone, Integer eventId, Integer eventType, String rewardName) {
        PageResult<UserRewardLog> pageResult = activityService.queryUserRewardLog(page, size, adAccountId, accountId, startTime, endTime, invitePhone, eventId, eventType, rewardName);
        return Result.success(pageResult);
    }

    /**
     * 奖励统计明细导出
     *
     * @param adAccountId 区域id
     * @param accountId   品牌id
     * @param startTime   开始时间
     * @param endTime     结束时间
     * @param invitePhone 用户手机号
     * @param eventId     活动id
     * @param eventType   活动类型
     * @param rewardName  优惠券名称
     * @return
     */
    @GetMapping("/exportUserRewardLog")
    public Result exportUserRewardLog(Integer adAccountId, Integer accountId, Date startTime, Date endTime, String invitePhone, Integer eventId, Integer eventType, String rewardName) {
        PageResult<UserRewardLog> pageResult = activityService.queryUserRewardLog(1, 65535, adAccountId, accountId, startTime, endTime, invitePhone, eventId, eventType, rewardName);
        List<UserRewardLog> list = pageResult.getList();
        ExcelUtil<UserRewardLog> excel = new ExcelUtil<>(UserRewardLog.class);
        JsonResult userRewardLog = excel.exportExcel(list, "奖励统计明细");
        return Result.success(userRewardLog);
    }


    /**
     * 用户排行榜
     *
     * @param page      当前页码
     * @param size      每页显示的行
     * @param accountId 品牌id
     * @param eventId   活动id
     * @return
     */
    @GetMapping("/queryUserRewardRank")
    public Result<PageResult<UserRewardRank>> queryUserRewardRank(@NotNull Integer page, @NotNull Integer size, @NotNull Integer accountId, Integer eventId) {
        PageResult<UserRewardRank> pageResult = activityService.queryUserRewardRank(page, size, accountId, eventId);
        return Result.success(pageResult);
    }

    /**
     * 获取品牌id
     *
     * @param accountId
     * @param request
     * @return
     */
    @GetMapping("/getBrandId")
    public Result getBrandId(HttpServletRequest request, Integer accountId) {
        LoginInfo login = BaseController.getLogin(request);
        Integer id = login.getId();
        //前端有传用前端的
        if (accountId != null) {
            id = accountId;
        }
        Integer brandId = accountService.getBrandId(id);
        if (brandId == -1) {
            return Result.success(id);
        }
        return Result.success(brandId);
    }

    /**
     * 获取品牌下的所有运营区域
     *
     * @param brandId 品牌id
     * @return
     */
    @GetMapping("/getArea")
    public Result<List<AreaVO>> getAreaByBrandId(Integer brandId) {
        List<AreaVO> list = accountService.getAreaByBrandId(brandId);
        return Result.success(list);
    }

}
